package LinkedList;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 21626
 * Date: 2024-10-24
 * Time: 13:30
 */
public class MySList {

    //静态内部类
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;

    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }
    public void show(){
        ListNode cur = head;
        while (cur!=null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    public int size(){
        int ret = 0;
        ListNode cur = head;
        while (cur!=null){
            ret++;
            cur = cur.next;
        }
        return ret;
    }
    public boolean contains(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;    //把头结点更新为node
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
            return;
        }
        while ((cur.next!=null)){
            cur = cur.next;
        }
        cur.next = node;
    }
    public void addIndex(int index,int val){
        if(index == 0){
            addFirst(val);
            return;
        }else if(index == size()-1){
            addLast(val);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(val);
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 找到index的前一个节点
     * @param index
     * @return
     */
    public ListNode findIndex(int index){
        ListNode cur = head;
        int count = 0;
        while (count!=index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }
    public void remove(int key){
        if(head == null){
            return;
        }
        if(head.val==key){
            head = head.next;
            return;
        }
        ListNode cur = findKey(key);
        if(cur == null){
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    private ListNode findKey(int key){
        ListNode cur = head;
        while (cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    public void clear(){
        ListNode cur = head;
        while (cur!=null){
            ListNode cur0 = cur.next;
            cur.next = null;
            cur = cur0;
        }
        head = null;
    }
}
